using System;
using System.Collections;
using System.IO;
using System.Linq;
using UnityEngine;

namespace BCW
{
	public static class BCWExtensions
	{
		public static string CombinePaths(params string[] paths)
		{
			if (paths == null)
			{
				throw new ArgumentNullException("paths");
			}
			Func<string, string, string> func = (string path1, string path2) => Path.Combine(path1, path2);
			return paths.Aggregate(func);
		}

		public static UIBasicSprite ReplaceForCurrentPlatform(this UIBasicSprite basicSprite)
		{
			if (basicSprite == null)
			{
				return null;
			}
			if (basicSprite.GetType() == typeof(UITextureMasked))
			{
				return basicSprite;
			}
			basicSprite.enabled = false;
			UITextureMasked uITextureMasked = basicSprite.gameObject.AddComponent<UITextureMasked>();
			uITextureMasked.width = basicSprite.width;
			uITextureMasked.height = basicSprite.height;
			uITextureMasked.depth = basicSprite.depth;
			uITextureMasked.color = basicSprite.color;
			uITextureMasked.mainTexture = basicSprite.mainTexture;
			uITextureMasked.uvRect = (basicSprite as UITexture).uvRect;
			UnityEngine.Object.Destroy(basicSprite);
			basicSprite = uITextureMasked;
			return basicSprite;
		}

		public static UIBasicSprite ReplaceToMasked(this UIBasicSprite sprite, bool masked = true)
		{
			Type type = sprite.GetType();
			if (masked && type == typeof(UITextureMasked))
			{
				return sprite;
			}
			if (!masked && type == typeof(UITexture))
			{
				return sprite;
			}
			sprite.enabled = false;
			UIBasicSprite uIBasicSprite = null;
			uIBasicSprite = ((!masked) ? ((UIBasicSprite)sprite.gameObject.AddComponent<UITexture>()) : ((UIBasicSprite)sprite.gameObject.AddComponent<UITextureMasked>()));
			uIBasicSprite.width = sprite.width;
			uIBasicSprite.height = sprite.height;
			uIBasicSprite.depth = sprite.depth;
			uIBasicSprite.color = sprite.color;
			(uIBasicSprite as UITextureMasked).uvRect = (sprite as UITexture).uvRect;
			UnityEngine.Object.Destroy(sprite);
			sprite = uIBasicSprite;
			return sprite;
		}

		public static void LoadAlphaMask(this UIBasicSprite sprite, string path, string fileName)
		{
			sprite.textureAlpha = Resources.Load<Texture>(path + "AlphaMask/" + fileName);
		}

		public static void LoadTextureAndAlphaMask(this UIBasicSprite sprite, string path, string fileName)
		{
			sprite.mainTexture = Resources.Load<Texture>(path + fileName);
			sprite.textureAlpha = Resources.Load<Texture>(path + "AlphaMask/" + fileName);
		}

		public static IEnumerator LoadTextureAndAlphaMaskAsync(this UIBasicSprite sprite, string path, string fileName)
		{
			ResourceRequest _mainTextureRequest2 = null;
			ResourceRequest _alphaMaskRequest2 = null;
			_mainTextureRequest2 = Resources.LoadAsync<Texture>(path + fileName);
			_alphaMaskRequest2 = Resources.LoadAsync<Texture>(path + "AlphaMask/" + fileName);
			while (!_mainTextureRequest2.isDone || (_alphaMaskRequest2 != null && !_alphaMaskRequest2.isDone))
			{
				yield return null;
			}
			sprite.mainTexture = _mainTextureRequest2.asset as Texture;
			sprite.textureAlpha = _alphaMaskRequest2.asset as Texture;
		}
	}
}
